package monitor.view.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import monitor.model.entity.GeneralEntity;

import com.google.appengine.api.datastore.Entity;

public class Utils {
	
	/**
	 * @return car id, when new car is created and stored in DB
	 */
	public static String generateCarId() {
		String carId = "";
		Random random = new Random();
		
		for (int i = 0; i < 10; i++) {
			carId = carId + random.nextInt(10);
		}
		return carId;
	}
	
	/**
	 * Fill data from generalEntity to gaeEntity. This is needed to store data in GAE DB.
	 * 
	 * @param gaeEntity used by DatastoreService API to store data in DB
	 * @param generalEntity
	 */
	public static void fillGAEEntity(Entity gaeEntity, GeneralEntity generalEntity) {
		Map<String, Object> attributes = generalEntity.getAttributes();
		fillGAEEntity(gaeEntity, attributes);
	}
	
	/**
	 * Fill gaeEntiti with data from Map<String, Object> attributes
	 * 
	 * @param gaeEntity used by DatastoreService API to store data in DB
	 * @param attributes data to be stored
	 */
	public static void fillGAEEntity(Entity gaeEntity, Map<String, Object> attributes) {
		Iterator<String> iterator = attributes.keySet().iterator();
		while(iterator.hasNext()) {
			String param = iterator.next();
			Object value = attributes.get(param);
			gaeEntity.setProperty(param, value);
		}
	}
}
